﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using Manager;
using Model;
using System.Data;
namespace HealthSystem
{
    public partial class MySchool :BasePage
    {
        HW_SchoolManager manager = new HW_SchoolManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                yearbox.DataSource = HealthSystem.ReportPage.ReportCofig.GetReportYear();
                yearbox.DataTextField = "YearName";
                yearbox.DataValueField = "YearCode";
                yearbox.DataBind();

                if (Request["yearcode"] != null)
                {
                    yearbox.SelectedValue = Request["yearcode"].ToString();
                }
                BindGrid();
            }
        }
        public void BindGrid()
        {
            string orgcode = string.Empty;
            DataTable list = new DataTable();
            
            if (Request["districtid"]!=null)
            {
                string syear = yearbox.SelectedValue + "1";
                list = manager.GetDistrictSchool(syear, Request["districtid"].ToString());
            }
            else
            {
                string syear = yearbox.SelectedValue + "1";
                orgcode = string.IsNullOrEmpty(Request["orgcode"]) ? CustomerProfile.OrgUser.OrgCode : Request["orgcode"].ToString();
                list = manager.GetMySchool(syear, orgcode);
            }
            this.Gird1.DataSource = list;
            this.Gird1.DataBind();
        }
        protected void yearcodebox_changed(object sender,EventArgs e)
        {
            BindGrid();
        }
        public void Grid1_DoubleClick(object sender, ExtAspNet.GridRowClickEventArgs e)
        {
            int index = e.RowIndex;
            string pid = this.Gird1.Rows[index].DataKeys[1].ToString();
            PageContext.RegisterStartupScript(Window1.GetShowReference("SubAdmin.aspx?pid=" + pid, "区县镇管理员"));
        }
        public void Window1_Close(object sender, EventArgs e)
        {

        }
        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            try
            {
                string schoolcode = Gird1.Rows[e.RowIndex].DataKeys[0].ToString();
                if (e.CommandName == "download")
                {
                //    string signDir = System.Configuration.ConfigurationManager.AppSettings["SignDir"].ToString();
                //    string fileName = "aaa.txt";//客户端保存的文件名
                //    string filePath = signDir + "\\" + schoolcode + ".xml";
                //    if (System.IO.File.Exists(filePath))
                //    {
                //        //以字符流的形式下载文件
                //        System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
                //        byte[] bytes = new byte[(int)fs.Length];
                //        fs.Read(bytes, 0, bytes.Length);
                //        fs.Close();
                //        Response.ContentType = "application/octet-stream";
                //        //通知浏览器下载文件而不是打开
                //        Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                //        Response.BinaryWrite(bytes);
                //        Response.Flush();
                //        HttpContext.Current.ApplicationInstance.CompleteRequest();
                //    }

                    string fileName = "asd.txt";//客户端保存的文件名
                    string signDir = System.Configuration.ConfigurationManager.AppSettings["SignDir"].ToString();
                    string filePath = signDir + "\\" + schoolcode + ".xml";
                    if (System.IO.File.Exists(filePath))
                    {

                        Response.ContentType = "application/x-zip-compressed";
                        Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
                        //string filename = Server.MapPath("DownLoad/z.zip");
                        Response.TransmitFile(filePath);

                        //System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
                        //Response.Clear();
                        //Response.ClearContent();
                        //Response.ClearHeaders();
                        //Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                        //Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                        //Response.AddHeader("Content-Transfer-Encoding", "binary");
                        //Response.ContentType = "application/octet-stream";
                        //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                        //Response.WriteFile(fileInfo.FullName);
                        //Response.Flush();
                        //HttpContext.Current.ApplicationInstance.CompleteRequest();
                    }
                    else
                    {
                        Alert.Show("证书不存在!请联系管理员");
                    }
                    //重置密码
                    //HW_OrgUserManager orgUserManager = new HW_OrgUserManager();
                    //HW_OrgUser orgUser = orgUserManager.GetById(username);
                    //orgUser.XX_Pass = System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                    //orgUserManager.ModHW_OrgUser(orgUser);
                    //Alert.Show("密码重置成功");
                    //BindGrid();
                }
            }
            catch(Exception ex)
            {
                Alert.Show(ex.Message);
            }
        }
    }
}